package com.ljt.springbootwebsocketdemo.starterType;

import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 对于连接的websocket的记录实现了增删改查
 */
public class WsSessionManager {
    private static ConcurrentHashMap<String, WebSocketSession> SESSION_POOL = new ConcurrentHashMap<>();

    public static void add(String key, WebSocketSession value){
        SESSION_POOL.put(key, value);
    }

    public static WebSocketSession get(String key){
        return SESSION_POOL.get(key);
    }
    private static WebSocketSession remove(String key) {
        return SESSION_POOL.remove(key);
    }

    public static void removeAndClose(String key) throws IOException {
        WebSocketSession session = remove(key);
        if (session == null) {
            return;
        }
        session.close();
    }


}
